import util from '@/libs/util';
import studentList from '../studentsignup/studentList.vue';
import regs from '../../util/formReg';

export default {
    components: {
        studentList
    },
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '学生姓名',
                key: 'studentName'
            }, {
                title: '充值/支出金额',
                key: 'amt',
                render: (h, params) => {
                    let pay_amt = params.row.amt;
                    let pay_type = params.row.type;
                    if (pay_type == 'PLUS') {
                        return h('span', {
                            style: {
                                color: 'green'
                            }
                        }, '+' + pay_amt);
                    } else if (pay_type == 'MINUS') {
                        return h('span', {
                            style: {
                                'color': 'red'
                            }
                        }, '-' + pay_amt);
                    }
                }
            }, {
                title: '赠送金额',
                key: 'giveAmt'
            }, {
                title: '创建时间',
                key: 'createTime',
                render: (h, params) => {
                    return h('span', {}, util.getDate(params.row.createTime, 'time'));
                }
            }, {
                title: '支付类型',
                key: 'payType',
                render: (h, params) => {
                    let payType = params.row.payType;
                    let text = '';
                    if (payType == 'CASH') {
                        text = '现金'
                    } else if (payType == 'WECHAT') {
                        text = '微信'
                    }
                    return h('span', {}, text);
                }
            }],
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            dataList: [],
            addOrEditVisible: false,
            studentModal: false,
            form: {
                name: ''
            },
            forms: {
                name: ''
            },
            queryData: {
                studentName: '',
                createTime: ''
            },
            formData: {
                studentName: '',
                type: '',
                createTime: '',
                amt: 0,
                giveAmt: 0
            },
            formDataRules: {
                studentName: [
                    {required: true, message: '请确认学生姓名', trigger: 'blur', type: 'string'}
                ],
                amt: [
                    {required: true, message: '请输入充值金额', trigger: 'blur', type: 'number'},
                    {required: true, validator: this.inputAmt, trigger: 'blur'}
                ],
                payType: [
                    {required: true, message: '请选择支付类型', trigger: 'blur', type: 'string'}
                ],
            },
            payNumVisible: false,
            payNum: '',
            orderDetails: {},
            totalAmt: 0,
            totalCost: 0,
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList: function () {
            util.ajax.post('/busine/balance/charge/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                studentName: this.queryData.studentName,
                startTime: this.queryData.startTime,
                endTime: this.queryData.endTime
            })).then(res => {
                if (res.data.code == 200) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                } else {
                    this.$Message.error(res.data.message);
                }
            });

            util.ajax.post('/busine/balance/charge/getPlusAndMinus', util.toFormData({
                studentName: this.queryData.studentName,
                startTime: this.queryData.startTime,
                endTime: this.queryData.endTime
            })).then(res => {
                if (res.data.code == 200) {
                    let data = res.data.data;
                    this.totalAmt = data.totalAmt;
                    this.totalCost = data.totalCost;
                } else {
                    this.$Message.error(res.data.message);
                }
            });

        },
        showAdd: function (obj) {
            this.addOrEditVisible = true;
            this.isRead = false;
            this.form = {};
            this.forms = {};
            this.formData = {
                studentName: '',
                amt: 0,
                giveAmt: 0,
                payType: '',
            };
        },
        refresh: function () {
            this.form = {
                name: ''
            };
            this.forms = {
                name: ''
            };
            this.queryData = {
                studentName: '',
                createTime: ''
            };
            this.loadList();
        },
        doSave: function (name) {
            this.$refs['formData'].validate((valid) => { // 表单验证
                if (valid) {
                    let formData = this.formData;
                    let payType = formData.payType;
                    let realAmt = formData.amt + formData.giveAmt;
                    this.orderDetails.real_amt = realAmt;
                    if (payType == 'CASH') {
                        util.ajax.post('/busine/balance/charge/save', util.toFormData(formData)).then(res => {
                            if (res.data.code == 200) {
                                this.$Message.success(res.data.message);
                                this.addOrEditVisible = false;
                                this.loadList();
                                this.$refs['formData'].resetFields();
                            } else {
                                this.$Message.error(res.data.message);
                            }
                        });
                    } else if (payType = 'WECHAT') {
                        util.ajax.post('/busine/balance/charge/save', util.toFormData(formData)).then(res => {
                            if (res.data.code == 200) {
                                this.addOrEditVisible = false;
                                this.$refs['formData'].resetFields();
                                this.loadList();
                                this.payNumVisible = true;
                                this.orderDetails.id = res.data.data.id;
                            } else {
                                this.$Message.error(res.data.message);
                            }
                        });

                    }
                } else {
                    this.$Message.warning("请检查输入项")
                }
            });
        },
        setStudent: function () {
            let student = this.$refs.student.student;
            this.formData.studentName = student.name;
            this.formData.studentId = student.id;
        },
        queryByName: function () {
            let name = this.formData.studentName;
            if (regs.isNotEmpty(name)) {
                util.ajax.post('/busine/student/queryByName', util.toFormData({
                    name: name
                })).then(res => {
                    if (res.data.code == 200) {
                        // noinspection JSAnnotator
                        if (res.data.data == '' || res.data.data == '' || res.data.data == undefined) {
                            // donothing
                        } else {
                            let data = res.data.data;
                            this.formData.studentName = data.name;
                            this.formData.studentId = data.id;
                            // this.form = res.data.data;
                            // this.form.name="123213"
                        }
                    } else {
                        this.$Message.error(res.data.message);
                    }
                });
            }
        },
        inputAmt: function (rule, value, callback, source) {
            let v = value;
            if (value <= 0) {
                callback(new Error('充值金额有误!'));
            } else {
                callback();
            }
        },
        payByWechat: function () {
            this.$Modal.confirm({
                title: '提示',
                content: '确认应收金额:' + this.orderDetails.real_amt + '(元)?',
                onOk: () => {
                    debugger;
                    this.payNumVisible = true;
                    let payNum = this.payNum;
                    if (payNum) {
                        this.$Message.loading({
                            content: '支付中请稍后...',
                            duration: 0,
                            closable: true
                        });
                        util.ajax.post('/busine/balance/charge/wechatPay', util.toFormData({
                            id: this.orderDetails.id,
                            payNum: payNum
                        })).then(res => {
                            if (res.data.code == 1000) { // 免密支付成功
                                this.$Message.destroy();
                                this.$Notice.open({
                                    title: '支付消息',
                                    desc: '支付成功！',
                                    duration: 0
                                });
                                this.orderDetails.id = res.data.data.id;
                                // this.cleanPayShit();
                                //
                                // let data = res.data.data;
                                // data.student_name = this.orderDetails.name;
                                // data.real_amt = this.orderDetails.real_amt;
                                // let params = {
                                //     data: data,
                                //     entry: 2// 入口
                                // };
                                // this.goInvoicePage(params);
                                // this.queryWechatResult();
                            } else if (res.data.code == 1001 || res.data.code == 100) {
                                this.$Message.destroy();
                                this.$Notice.open({
                                    title: '支付消息',
                                    desc: res.data.message,
                                    duration: 0
                                });
                                // this.cleanPayShit();
                            } else if (res.data.code == 1002) {
                                this.queryWechatResult();
                            }
                        });
                    }
                },
                onCancel: () => {

                }
            });
        },
        queryWechatResult: function () {
            util.ajax.post('/busine/balance/charge/wechatQuery', util.toFormData({
                id: this.orderDetails.id
            })).then(res => {
                if (res.data.code == 1000) {
                    this.$Message.destroy();
                    this.$Notice.open({
                        title: '支付消息',
                        desc: '支付成功!',
                        duration: 0
                    });
                    let data = res.data.data;
                    // data.student_name = this.orderDetails.name;
                    // data.real_amt = this.orderDetails.real_amt;
                    // let params = {
                    //     data: data,
                    //     entry: 2// 入口
                    // };
                    // this.goInvoicePage(params);
                } else if (res.data.code == 500 || res.data.code == 100) { // 业务逻辑错误
                    this.$Notice.open({
                        title: '支付消息',
                        desc: res.data.message,
                        duration: 0
                    });
                    // this.cleanPayShit();
                } else {
                    this.queryWechatResult();
                }
            });
        },
    }
};
